package com.nebula.tlggoods.service.impl;

import com.nebula.tlggoods.entity.GoodsType;
import com.nebula.tlggoods.mapper.GoodsTypeMapper;
import com.nebula.tlggoods.service.GoodsTypeService;
import com.nebula.tlggoods.tools.Record;
import com.nebula.tlggoods.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 分类实现类
 */
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {


    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 一级分类
     *
     * @return
     */
    @Override
    public Record findOneClassify() {
        Record record = new Record();
        List<GoodsType> list = null;
        String key = "oneClassify";
        ValueOperations<String, List<GoodsType>> operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = goodsTypeMapper.findOneClassify();
                //添加緩存 5分钟
                operations.set(key, list, 5, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("一级分类查询异常");
            e.printStackTrace();
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("一级分类查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("一级分类查询失败");
        }
        return record;
    }

    /**
     * 根据一级分类id查询二级分类三级分类
     *
     * @param parentId 一级分类id
     * @return
     */
    @Override
    public Record findTwoClassifyAndThreeClassify(Integer parentId) {
        Record record = new Record();
        List<GoodsType> list = null;
        String key = "twoClassifyAndThreeClassify" + parentId;
        ValueOperations<String, List<GoodsType>> operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = goodsTypeMapper.findTwoClassifyAndThreeClassify(parentId);
                //添加緩存 3分钟
                operations.set(key, list, 3, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("二级分类及三级分类查询异常");
            e.printStackTrace();
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("二级分类及三级分类查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("二级分类及三级分类查询失败");
        }
        return record;
    }
}
